#include "EpilSimpleExpressionParser.h"
#include "EpilPrimaryExpressionParser.h"
#include "EpilDecimalExpressionCode.h"
#include "EpilStringExpressionCode.h"
#include "EpilParserUtilities.h"
#include "EpilNilExpressionCode.h"
#include "EpilBooleanExpressionCode.h"
#include "EpilTableConstructorParser.h"
#include "EpilFunctionBodyParser.h"
#include "EpilVariousParameterExpressionCode.h"
#include "EpilDoubleExpressionCode.h"
#include "EpilOctalExpressionCode.h"
#include "EpilHexadecimalExpressionCode.h"
#include "EpilScientificExpressionCode.h"


EpilSimpleExpressionParser::EpilSimpleExpressionParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilSimpleExpressionParser::Parse()
{
	const EpilToken & curToken = this->GetTokens().GetCurrentToken();

	switch( curToken.type )
	{
	case ETT_Decimal:
		{
			EpilDecimalExpressionCode * intExpr = new EpilDecimalExpressionCode( curToken.location );
			intExpr->SetValue( EpilParserUtilities::ParseDecimal( curToken.text ) );
			m_expression = intExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_Double:
		{
			EpilDoubleExpressionCode * doubleExpr = new EpilDoubleExpressionCode( curToken.location );
			doubleExpr->SetValue( EpilParserUtilities::ParseDouble( curToken.text ) );
			m_expression = doubleExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_Octal:
		{
			EpilOctalExpressionCode * octExpr = new EpilOctalExpressionCode( curToken.location );
			octExpr->SetValue( EpilParserUtilities::ParseOctal( curToken.text ) );
			m_expression = octExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_Hexadecimal:
		{
			EpilHexadecimalExpressionCode * hexExpr = new EpilHexadecimalExpressionCode( curToken.location );
			hexExpr->SetValue( EpilParserUtilities::ParseHexadecimal( curToken.text ) );
			m_expression = hexExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_Scientific:
		{
			EpilScientificExpressionCode * scientificExpr = new EpilScientificExpressionCode( curToken.location );
			scientificExpr->SetValue( EpilParserUtilities::ParseScientific( curToken.text ) );
			m_expression = scientificExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_String:
		{
			EpilStringExpressionCode * strExpr = new EpilStringExpressionCode( curToken.location );
			strExpr->SetString( curToken.text );
			m_expression = strExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_KwNil:
		{
			EpilNilExpressionCode * nilExpr = new EpilNilExpressionCode( curToken.location );
			m_expression = nilExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_KwTrue:
		{
			EpilBooleanExpressionCode * boolExpr = new EpilBooleanExpressionCode( curToken.location );
			boolExpr->SetValue( true );
			m_expression = boolExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_KwFalse:
		{
			EpilBooleanExpressionCode * boolExpr = new EpilBooleanExpressionCode( curToken.location );
			boolExpr->SetValue( false );
			m_expression = boolExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_OpVariousParameters:
		{
			EpilVariousParameterExpressionCode * vpExpr = new EpilVariousParameterExpressionCode( curToken.location );
			m_expression = vpExpr;
			this->GetTokens().MoveToNext();
		}
		break;
	case ETT_OpenBrace:
		{
			EpilTableConstructorParser constructorParser( this->GetTokens() );
			constructorParser.Parse();
			m_expression = constructorParser.GetConstructor();
		}
		break;
	case ETT_KwFunction:
		{
			this->GetTokens().MoveToNext();
			EpilFunctionBodyParser funcBodyParser( this->GetTokens() );
			funcBodyParser.Parse();
			EpilFunctionBodyExpressionCode * funcBody = funcBodyParser.GetBody();
			m_expression = funcBody;
		}
		break;
	default:
		{
			EpilPrimaryExpressionParser primaryExprParser( this->GetTokens() );
			primaryExprParser.Parse();
			m_expression = primaryExprParser.GetExpression();
		}
	}
}